﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using AvalonDock;
using System.Collections.ObjectModel;
using RiskMan.DataClass;
using RiskMan.Config;
using RiskMan.Providers.QuikIO;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Windows.Media;
using RiskMan.Views.AboutFrom;
using RiskMan.Views.Dialogs;
using QuikIntegration.dde;
using RiskMan.Views.Stop;


namespace RiskMan
{
    public partial class MainWindow : Window
    {
        public static MainWindow StaticLevel;
        
        //private Register reg;

        public ObservableCollection<DocumentContent> MyDocuments { get; private set; }
        public Data _dataProvider;
        public QuikTerminals Terminals;
        private History _history;
        public String path = "";
        List<string> listWindow = new List<string>();


        public DateTime startNight = new DateTime();
        public DateTime endNight = new DateTime();

        public MainWindow()
        {
            StaticLevel = this;
            try
            {
                InitializeComponent();
                MyDocuments = new ObservableCollection<DocumentContent>();
                DataContext = this;

                _dataProvider = new Data();
                _history = new History(_dataProvider);
                _history.Load();

                if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                {
                    QuikUtil.StopDDE();
                    QuikUtil.StartDDE();
                }

                #region загрузка вермени и тп
                try
                {
                    MenuItem_CheckForExit.IsChecked = Properties.Settings.Default.CanClose;
                    DateTime startD = Properties.Settings.Default.StartDay;
                    DateTime endD = Properties.Settings.Default.EndDay;

                    DateTime startN = Properties.Settings.Default.StartNight;
                    DateTime endN = Properties.Settings.Default.EndNight;

                    Properties.Settings.Default.StartDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, startD.Hour, startD.Minute, 0);
                    Properties.Settings.Default.EndDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, endD.Hour, endD.Minute, 0);

                    Properties.Settings.Default.StartNight = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, startN.Hour, startN.Minute, 0);
                    Properties.Settings.Default.EndNight = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, endN.Hour, endN.Minute, 0);

                    this.startNight = Properties.Settings.Default.StartNight;
                    this.endNight = Properties.Settings.Default.EndNight;
                }
                catch (Exception)
                {
                    new DialogOkCancel("При инициализации произоша ошибка", "Ошибка");
                }
                #endregion

            }
            catch (Exception ex)
            {
                new DialogOkCancel("При инициализации произоша ошибка: " + ex.Source, "Ошибка");
            }

        }


        public void dockManager_Loaded(object sender, RoutedEventArgs args)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //string uid = Cfg.GetID() + Cfg.GetMacAddress();
            //reg = new Register(uid);

            dockManager.Loaded += new RoutedEventHandler(dockManager_Loaded);

            try
            {
                if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                    if (_dataProvider.T2QItems.Count <= 0)
                    {
                        if ((new DialogYesNo("Вы хотите настроить соединения сейчас?", "Загрузка")).Result == MessageBoxResult.Yes)
                        {
                            MainSettings st = new MainSettings() { Name = "Settings", Title = "Настройки" };
                            MyDocuments.Add(st);

                            st.Activate();
                        }
                    }
                //this.LoadPlugins( ); // загрузка плагинов
            }
            catch (Exception ex)
            {
                new DialogOkCancel(ex.ToString(), "Window_Loaded");
            }

            listWindow = GetWindow();
            foreach (var item in listWindow)
            {
                PublishNewDocument(item);
            }
        }

        private List<string> GetWindow()
        {
            return (List<string>)XmlHistory.GetXmlData(XmlHistory.FilenameWindow);
        }

        private void CreateNewDocument(object sender, RoutedEventArgs e)
        {
            string baseDocTitle = ((MenuItem)sender).Header.ToString();
            for (int i = 0; i < dockManager.Documents.Count; i++)
            {
                if (dockManager.Documents[i].Title == baseDocTitle)
                {
                    dockManager.Documents[i].Activate();
                    return;
                }
            }
            PublishNewDocument(baseDocTitle);

        }

        private void PublishNewDocument(string baseDocTitle)
        {

            if (baseDocTitle == Cfg.Mainsetting)
            {
                MainSettings uc = new MainSettings() { Name = "Settings", Title = baseDocTitle, Background = new SolidColorBrush(new Color() { R = 145, G = 158, B = 167 }) };
                MyDocuments.Add(uc);
                uc.Activate();
            }

            if (baseDocTitle == Cfg.Stops)
            {
                StopView uc = new StopView() { Name = Cfg.Stops, Title = baseDocTitle, Background = new SolidColorBrush(new Color() { R = 145, G = 158, B = 167 }) };
                MyDocuments.Add(uc);
                uc.Activate();
            }
        }

        private void ClearDocumentsList(object sender, RoutedEventArgs e)
        {
            MyDocuments.Clear();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            listWindow.Clear();
            foreach (var item in MyDocuments)
            {
                listWindow.Add(item.Title);
            }
            SetWindow(listWindow);

            if (Properties.Settings.Default.adapter == Cfg.AdapterAlfa)
            {

            }
        }

        private void SetWindow(List<string> list)
        {
            XmlHistory.SetXmlData(XmlHistory.FilenameWindow, (Object)(list));
        }

        private void Button1_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.Topmost == false)
            {
                this.Topmost = true;
                Button1.Style = (Style)this.Resources["TopMost_ButtonStyle"];
                Cfg.SetLog(this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + ": Свойсвтво окна \"по верх всех\" - поставлено", MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
            else
            {
                this.Topmost = false;
                Button1.Style = (Style)this.Resources["TopMostOut_ButtonStyle"];
                Cfg.SetLog(this.GetType().FullName + "." + MethodBase.GetCurrentMethod().Name + ": Свойсвтво окна \"по верх всех\" - снято", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        public void Terminals_OnTerminalsConnectionChanged(String msgError, String name, bool isConnect, object sender)
        {
            //cfg.SetLog( "Terminal:  " + name + " Connection:  " + sender.ToString( ) + ":  " + msgError, ListBox_Logs );
            if (sender.ToString() == "SUCCESS" || sender.ToString() == "QUIK_CONNECTED")
            {
                for (int i = 0; i < _dataProvider.T2QItems.Count; i++)
                {
                    if (_dataProvider.T2QItems[i].Name == name)
                        _dataProvider.T2QItems[i].IsConn = true;
                }
            }
            else
            {
                for (int i = 0; i < _dataProvider.T2QItems.Count; i++)
                {
                    if (_dataProvider.T2QItems[i].Name == name)
                        _dataProvider.T2QItems[i].IsConn = false;
                }
            }

            if (sender.ToString() == "DLL_DISCONNECTED")
            {
                Cfg.SetLog(" Соединение с терминалом \"" + name + "\" потеряно", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
                new DialogMessage("Соединение с терминалом \"" + name + "\" потеряно", "Внимание");
            }

            if (sender.ToString() == "QUIK_DISCONNECTED")
                Cfg.SetLog(" Соединение терминала \"" + name + "\" с сервером отсутствует", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());

            if (sender.ToString() == "QUIK_CONNECTED")
                Cfg.SetLog(" Соединение терминала \"" + name + "\" с сервером установлено", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());

            if (sender.ToString() == "SUCCESS")
                Cfg.SetLog(" Соединение с терминалом \"" + name + "\" установлено", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());

            if (sender.ToString() == "QUIK_TERMINAL_NOT_FOUND")
                Cfg.SetLog(" Терминал \"" + name + "\" не запушен. Проверьте включены ли внешние транзакции", ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());

            Debug.Print(sender.ToString());
        }

        public void Terminals_OnTerminalsTransaction(object error, int tid, double orderId, string msgError)
        {
            //cfg.SetLog(": TId:  " + tid + "  OrderId:  " + orderId + "  Error:  " + error + " Comment:  " + msgError, ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            if (orderId == 0)
            {
                //_dataProvider.OrderBlockingItems = new OBlockingItems().ToList(_dataProvider.OrderBlockingItems.Where(a => a.TID != tid).ToList());
                //_dataProvider.StopOrderBlockingItems = new SOBlockingItems().ToList(_dataProvider.StopOrderBlockingItems.Where(a => a.TID != tid).ToList());
            }

        }

        public void Terminals_OnTerminalsTransactionAcc(object error, int tid, double orderId, string msgError, string acc)
        {
            //cfg.SetLog("Cчет: " + acc + ";  ID транзакции:  " + tid + ";  № заявки:  " + orderId + ";  Статус:  " + error + ";  Комментарий:  " + msgError, ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            Cfg.SetLog("Cчет: " + acc + ";  Комментарий:  " + msgError, ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            if (orderId == 0)
            {
                //_dataProvider.OrderBlockingItems = new OBlockingItems().ToList(_dataProvider.OrderBlockingItems.Where(a => a.TID != tid).ToList());
                //_dataProvider.StopOrderBlockingItems = new SOBlockingItems().ToList(_dataProvider.StopOrderBlockingItems.Where(a => a.TID != tid).ToList());
            }

        }


        /// <summary>
        /// создание транзакционного соединия 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void button2_Click(object sender, RoutedEventArgs e)
        {
            //if (reg.IsRun == false)
            //{
            //    if (reg.message.Length != 0)
            //        new DialogOkCancel(reg.message, "Регистрация");
            //    else
            //        new DialogOkCancel("Вы не зарегистрированы", "Регистрация");
            //    return;
            //}

            try
            {

                if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                {
                    if (_dataProvider.T2QItems.Count > 0)
                    {
                        if (Terminals == null)
                        {
                            Terminals = new QuikTerminals(_dataProvider);
                            Terminals.OnTerminalsConnectionChanged += new QuiksStatusEventHandler(Terminals_OnTerminalsConnectionChanged);
                            Terminals.OnTerminalsTransaction += new QuiksTransEventHandler(Terminals_OnTerminalsTransaction);
                            Terminals.OnTerminalsTransactionAcc += new QuiksTransEventHandlerAcc(Terminals_OnTerminalsTransactionAcc);

                            ProcessCheck tmp = new ProcessCheck();
                            tmp.Check();

                            Terminals.Connects(this.ListBox_Logs);

                            btn_Connect.IsEnabled = false;
                            btnDisconnect.IsEnabled = true;
                            MenuItem_RunConnect.IsEnabled = false;
                            MenuItem_RunDisconnect.IsEnabled = true;
                        }
                    }
                    else
                    {
                        new DialogOkCancel("Настроеных терминалов нет. Добавьте и попробуйте снова", "Ошибка");
                    }
                }

            }
            catch (Exception ex)
            {
                Cfg.SetLog(ex.Message, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }

        #region icon to tray

        private System.Windows.Forms.NotifyIcon TrayIcon = null;
        private ContextMenu TrayMenu = null;
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            createTrayIcon();
        }

     
        private bool createTrayIcon()
        {
            bool result = false;

            if (TrayIcon == null)
            {
                TrayIcon = new System.Windows.Forms.NotifyIcon();
                TrayIcon.Icon = RiskMan.Properties.Resources.linechart_8075;
                TrayIcon.Text = "Уровневый робот";

                TrayMenu = Grid_Temp.Resources["TrayMenu"] as ContextMenu;

                TrayIcon.Click += delegate(object sender, EventArgs e)
                {
                    if ((e as System.Windows.Forms.MouseEventArgs).Button == System.Windows.Forms.MouseButtons.Left)
                    {
                        ShowHideMainWindow(sender, null);
                    }
                    else
                    {
                        TrayMenu.IsOpen = true;
                        Activate();
                    }
                };
                result = true;
            }
            else
            {
                result = true;
            }

            TrayIcon.Visible = true;
            return result;
        }

        private void ShowHideMainWindow(object sender, RoutedEventArgs e)
        {
            TrayMenu.IsOpen = false; // спрячем менюшку, если она вдруг видима
            if (IsVisible)
            {
                Hide();
                (TrayMenu.Items[0] as MenuItem).Header = "Показать";
            }
            else
            {
                Show();
                (TrayMenu.Items[0] as MenuItem).Header = "Скрыть";
                WindowState = CurrentWindowState;
                Activate();
            }
        }

        private WindowState fCurrentWindowState = WindowState.Normal;
        public WindowState CurrentWindowState
        {
            get { return fCurrentWindowState; }
            set { fCurrentWindowState = value; }
        }

        protected override void OnStateChanged(EventArgs e)
        {
            base.OnStateChanged(e);
            if (this.WindowState == System.Windows.WindowState.Minimized)
            {
                //Hide( );
                (TrayMenu.Items[0] as MenuItem).Header = "Показать";
            }
            else
            {
                CurrentWindowState = WindowState;
            }
        }





        private bool fCanClose;
        public bool CanClose
        {
            get { return fCanClose; }
            set { fCanClose = value; }
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {

            base.OnClosing(e);
            if (RiskMan.Properties.Settings.Default.CanClose)
            {
                e.Cancel = true;
                CurrentWindowState = this.WindowState;
                (TrayMenu.Items[0] as MenuItem).Header = "Показать";

                Hide();
            }
            else
            {
                if ((new DialogYesNo("Вы действительно хотите выйти?", "Внимание!")).Result == MessageBoxResult.Yes)
                {
                    if ((new DialogYesNo("Сохранить данные?", "Внимание!")).Result == MessageBoxResult.Yes)
                    {
                        _history.Save();
                    }
                    TrayIcon.Visible = false;
                }
                else
                    e.Cancel = true;
            }
        }



        private void MenuExitClick(object sender, RoutedEventArgs e)
        {
            if ((new DialogYesNo("Вы действительно хотите выйти?", "Внимание!")).Result == MessageBoxResult.Yes)
            {
                if ((new DialogYesNo("Сохранить данные?", "Внимание!")).Result == MessageBoxResult.Yes)
                {
                    _history.Save();
                }
            }
            else
                return;

            TrayIcon.Visible = false;
            Environment.Exit(9);
            //  Close( );
        }

        #endregion

        /// <summary>
        /// Кнопка Создания вывода по dde каналам
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click_1(object sender, RoutedEventArgs e)
        {
            try
            {
                QuikUtil.StopDDE();

                _dataProvider.Clear();


                QuikUtil.StartDDE();
            }
            catch (Exception ex)
            {
                new DialogOkCancel(ex.ToString(), "button3_Click_1");
            }
            
        }

        private void MenuItem_SaveSettings_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _history.Save();
            }
            catch (Exception ex)
            {
                new DialogOkCancel(ex.ToString(), "MenuItem_SaveSettings_Click");
            }
        }

        private void MenuItem_CheckForExit_Click(object sender, RoutedEventArgs e)
        {
            RiskMan.Properties.Settings.Default.CanClose = MenuItem_CheckForExit.IsChecked;
            RiskMan.Properties.Settings.Default.Save();
        }

        #region Disconnect Trans2Quik
        private void button4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (_dataProvider.T2QItems.Count > 0)
                {
                    if (new DialogYesNo("Вы действительно хотите хотите разорвать \nвсе имеющиеся соединения?", "Внимание!").Result == MessageBoxResult.Yes)
                    {
                        Terminals.Disconnect();
                        Terminals = null;

                        btnDisconnect.IsEnabled = false;
                        btn_Connect.IsEnabled = true;
                        MenuItem_RunConnect.IsEnabled = true;
                        MenuItem_RunDisconnect.IsEnabled = false;

                        Cfg.SetLog("Все имеющиеся соединения разорваны", GetType(), MethodBase.GetCurrentMethod());
                    }
                }
                else
                {
                    new DialogOkCancel("Настроеных терминалов нет. Добавьте и попробуйте снова", "Ошибка");
                }
            }
            catch (Exception ex)
            {
                Cfg.SetLog(ex.Message, MainWindow.StaticLevel.ListBox_Logs, GetType(), MethodBase.GetCurrentMethod());
            }
        }
        #endregion

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        /// <summary>
        /// About form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MenuItem_Click_2(object sender, RoutedEventArgs e)
        {
            About about = new About();
            about.ShowDialog();
        }

        private void MenuItem_LogFolder_Click(object sender, RoutedEventArgs e)
        {
            String path = Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + "\\Robot\\";
            if (Directory.Exists(path))
                Process.Start("explorer", path);
        }

        /// <summary>
        /// Определение подключенных терминалов есть или нет
        /// </summary>
        /// <returns></returns>
        public bool ConnectingOrDisconnecting()
        {
            bool running = false;
            try
            {
                if (Properties.Settings.Default.adapter == Cfg.AdapterQuik)
                    if (this.Terminals == null)
                    {
                        running = false;

                    }
                    else
                    {
                        if (this.Terminals.CountConnected() > 0)
                            running = true;
                        else
                            running = false;
                    }
            }
            catch
            {
            }
            return running;
        }
    }
}